#include <gtkmm.h>
#include <iostream>
#include <unistd.h>

Gtk::Window* p_main_window = 0;
Gtk::ToolButton* ptool_new = 0;
Gtk::ToolButton* ptool_cancel = 0;
Gtk::ToolButton* ptool_resume = 0;
Gtk::ToolButton* ptool_pause = 0;
Gtk::ToolButton* ptool_about = 0;
Gtk::ImageMenuItem* p_menu_new = 0;
Gtk::ImageMenuItem* p_menu_exit = 0;
Gtk::ImageMenuItem* p_menu_about = 0;
Gtk::ImageMenuItem* p_delay_exit = 0;
Gtk::ImageMenuItem* p_delay_logout = 0;
Gtk::AboutDialog* p_about = 0;
Gtk::Label* p_perc = 0;
Gtk::Label* p_prog = 0;
Gtk::Label* p_speed = 0;

#include "read_main.h"

bool on_timeout()
{
    int ret = read();
    if(1 == ret || 2 == ret )
        return false;
    
    return true;
}

void on_new_clicked()
{
	system("./D_handler &");
	usleep(3000000);		//sleep for 3 sec
	Glib::signal_timeout().connect(sigc::ptr_fun(on_timeout), 500 );
}

void on_quit()
{
	p_main_window->hide();
	delete p_main_window;
	exit(0);
}

bool logout_complete()
{
    system("if [[ `pgrep aria2c` = \"\" ]]; then logout; fi");
    
    return true;
}

void logout()
{
    Glib::signal_timeout().connect(sigc::ptr_fun(logout_complete), 2000 );
}

bool delay_exit_complete()
{
    system("if [[ `pgrep aria2c` = \"\" ]]; then pkill ldm_main; fi");
    
    return true;
}

void delay_exit()
{
    Glib::signal_timeout().connect(sigc::ptr_fun(delay_exit_complete), 500 );
}

void on_about_dialog_response(int response_id)
{
	if((response_id == Gtk::RESPONSE_CLOSE) || (response_id == Gtk::RESPONSE_CANCEL) )
  	{
    	p_about->hide();
  	}
}

void open_dialog()
{
	p_about->show();
}

void on_cancel_clicked()
{
    system("pkill --signal SIGKILL aria2c");
}

void on_resume_clicked()
{
    system("pkill --signal SIGCONT aria2c");
}

void on_pause_clicked()
{
    system("pkill --signal SIGSTOP aria2c");
}

int main (int argc, char **argv)
{
  Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "code.google.ldm");

  Glib::RefPtr<Gtk::Builder> refBuilder = Gtk::Builder::create();
  refBuilder->add_from_file("main.glade");

  refBuilder->get_widget("ldm_main", p_main_window);
  if(p_main_window)
  {
    // ToolButtons
    refBuilder->get_widget("toolbutton1", ptool_new);
    	ptool_new->signal_clicked().connect( sigc::ptr_fun(on_new_clicked) );
    refBuilder->get_widget("toolbutton2", ptool_cancel);
    	ptool_cancel->signal_clicked().connect( sigc::ptr_fun(on_cancel_clicked) );
    refBuilder->get_widget("toolbutton4", ptool_resume);
    	ptool_resume->signal_clicked().connect( sigc::ptr_fun(on_resume_clicked) );
    refBuilder->get_widget("toolbutton5", ptool_pause);
    	ptool_pause->signal_clicked().connect( sigc::ptr_fun(on_pause_clicked) );
    refBuilder->get_widget("toolbutton7", ptool_about);
    	ptool_about->signal_clicked().connect( sigc::ptr_fun(open_dialog) );
   
    // MenuButtons
    refBuilder->get_widget("menu_new", p_menu_new);
    	p_menu_new->signal_activate().connect(sigc::ptr_fun(on_new_clicked));
    refBuilder->get_widget("menu_quit", p_menu_exit);
    	p_menu_exit->signal_activate().connect( sigc::ptr_fun(on_quit) );
    refBuilder->get_widget("menu_about", p_menu_about);
    	p_menu_about->signal_activate().connect(sigc::ptr_fun(open_dialog));
    refBuilder->get_widget("delay_exit", p_delay_exit);
    	p_delay_exit->signal_activate().connect(sigc::ptr_fun(delay_exit));
    refBuilder->get_widget("delay_logout", p_delay_logout);
    	p_delay_logout->signal_activate().connect(sigc::ptr_fun(logout));
    
    // Progress_Labels
    refBuilder->get_widget("perc", p_perc);
        p_perc->set_label(" ");
    refBuilder->get_widget("prog", p_prog);
        p_prog->set_label(" ");
    refBuilder->get_widget("speed", p_speed);
        p_speed->set_label(" ");    

    // About Dialog
    refBuilder->get_widget("aboutdialog", p_about);
    	p_about->signal_response().connect( sigc::ptr_fun(on_about_dialog_response) );

    app->run(*p_main_window);
  }

  delete p_main_window;

  return 0;
}
